package com.ws.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.ws.bean.*;
import com.ws.bean.vo.RouteVo;
import com.ws.service.CategorieService;
import com.ws.service.RouteService;
import com.ws.service.SupplierService;
import com.ws.service.ThemeService;
import com.ws.utils.Res;
import com.ws.utils.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/route")
public class RouteController {
    @Autowired
    private RouteService routeService;//线路service
    @Autowired
    private ThemeService themeService; //主题
    @Autowired
    private CategorieService categorieService;//分类
    @Autowired
    private SupplierService supplierService;//供应商


    //查询
    @RequestMapping("list")
    public String list(Model model , RouteVo routeVo) {
        PageHelper pageHelper = new PageHelper();
        pageHelper.startPage(routeVo.getCurrentPage(),routeVo.getPageSize());
        List<Routes> list = routeService.list(routeVo);
        PageInfo pageInfo = new PageInfo(list);
        //主题
        List<Themes> ThemeList = themeService.list();
        model.addAttribute("ThemeList", ThemeList);
        //分类
        List<Categories> CategorieList = categorieService.list();
        model.addAttribute("CategorieList", CategorieList);
        //供应商
        List<Suppliers> SupplierList = supplierService.list(null);
        model.addAttribute("SupplierList", SupplierList);

        model.addAttribute("pageInfo",pageInfo);
        model.addAttribute("routeVo",routeVo);
        return "route/list";
    }
    // 去添加页面
    @RequestMapping("toAdd")
    public String toAdd(Model model ) {
        //主题
        List<Themes> ThemeList = themeService.list();
        model.addAttribute("ThemeList", ThemeList);
        //分类
        List<Categories> CategorieList = categorieService.list();
        model.addAttribute("CategorieList", CategorieList);
        //供应商
        List<Suppliers> SupplierList = supplierService.list(null);
        model.addAttribute("SupplierList", SupplierList);
        return "route/add";
    }
    @RequestMapping("add")
    public String add(Routes routes, MultipartFile image) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            routes.setRoute_image(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));
        }
        routeService.add(routes);
        return "redirect:list";
    }

    //删除
    @RequestMapping("delete")
    public String delete(Integer route_id){
        routeService.delete(route_id);
        return "redirect:list";
    }
    //修改取消删除
    @RequestMapping("updateDele")
    public String updateDele(Integer route_id){
        routeService.updateDele(route_id);
        return "redirect:list";
    }
    @RequestMapping("toUpdate")
    public String toUpdate(Integer route_id,Model model){
        Routes routes = routeService.getRouteById(route_id);
        model.addAttribute("route",routes);
        //主题
        List<Themes> ThemeList = themeService.list();
        model.addAttribute("ThemeList", ThemeList);
        //分类
        List<Categories> CategorieList = categorieService.list();
        model.addAttribute("CategorieList", CategorieList);
        //供应商
        List<Suppliers> SupplierList = supplierService.list(null);
        model.addAttribute("SupplierList", SupplierList);
        return "route/update";
    }
    @RequestMapping("update")
    public String update(Routes routes, MultipartFile image,String img) throws IOException {
        if (image.getSize()>0){
            String uuid = UUID.randomUUID().toString().replaceAll("-", "");
            String houZhui = image.getOriginalFilename().split("\\.")[1];
            routes.setRoute_image(uuid+"."+houZhui);
            image.transferTo(new File(Res.fileDir+uuid+"."+houZhui));

            Routes oldRoute = routeService.getRouteById(routes.getRoute_id());
            String oldImg = oldRoute.getRoute_image();
            if (oldImg!=null&&!oldImg.equals(routes.getRoute_image())){
                File oldFile = new File(Res.fileDir+oldImg);
                if (oldFile.exists()){
                    oldFile.delete();
                }
            }
        }else{
            routes.setRoute_image(img);
        }
        routeService.update(routes);
        return "redirect:list";
    }



}
